package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Account;


/**
 * Servlet implementation class ServletUpdate
 */
public class ServletUpdate extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletUpdate() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		try{
				
			if (request.getMethod().equalsIgnoreCase("post")) {
				try {
					Class.forName("oracle.jdbc.driver.OracleDriver");
					Connection conn = DriverManager
							.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","a123");
					PreparedStatement st = conn
							.prepareStatement("Update Account Set pass = ? Where username = ?");
					st.setString(1, request.getParameter("password"));
					st.setString(2, request.getParameter("userName"));
					st.execute();
					st.close();
					conn.close();
					HttpSession session = request.getSession(true);
					Account acc = (Account)session.getAttribute("acc");
					if(acc == null){
						acc = new Account();
					//	acc.setStrUserName(request.getParameter("userName"));
						//acc.setStrPassword(request.getParameter("password"));
					}else{
						//acc.setStrPassword(request.getParameter("password"));
					}
					response.sendRedirect("ManageAccount/ReadAccount.jsp");
				} catch (Exception e) {
					System.out.print("Connection unsuccessfly");
				}
			}
		}catch(Exception ex){
			out.print(ex.getMessage());
		}
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}

}
